﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Values.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <BorderGapMaskConverter x:Key="BorderGapMaskConverter" />

    <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">

        <Setter Property="Foreground" Value="{StaticResource Menu.BorderBrush.Normal}" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Rectangle Width="{TemplateBinding Width}" Fill="{TemplateBinding Foreground}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">

        <Border x:Name="border"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                CornerRadius="{StaticResource Control.Border.CornerRadius}"
                Padding="{TemplateBinding Padding}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition x:Name="middleRow" Height="{StaticResource View.Region.Spacing.Small}" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <ContentPresenter x:Name="Icon"
                                  Width="{StaticResource Menu.Icon.Size.Value}"
                                  Height="{StaticResource Menu.Icon.Size.Value}"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  ContentSource="Icon" />
                <!--
                    <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center">
                    <Path x:Name="GlyphPanel"
                    Fill="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
                    Data="{StaticResource ResourceKey=Checkmark}"
                    Visibility="Collapsed"
                    Margin="0,0,4,0"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center" />
                -->
                <ContentPresenter x:Name="header"
                                  Grid.Row="2"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  ContentSource="Header"
                                  RecognizesAccessKey="True">
                    <ContentPresenter.Margin>
                        <Thickness Left="{StaticResource View.Region.Spacing.SmallValue}" Right="{StaticResource View.Region.Spacing.SmallValue}" />
                    </ContentPresenter.Margin>
                </ContentPresenter>
                <!--  </StackPanel>  -->
            </Grid>
        </Border>

        <ControlTemplate.Triggers>

            <Trigger Property="Command" Value="{x:Null}">
                <Setter Property="IsEnabled" Value="False" />
            </Trigger>

            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="middleRow" Property="Height" Value="0" />
            </Trigger>

            <Trigger Property="Header" Value="{x:Null}">
                <Setter TargetName="header" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="middleRow" Property="Height" Value="0" />
            </Trigger>

            <Trigger Property="IsChecked" Value="true">
                <!--
                    <Setter TargetName="GlyphPanel"
                    Property="Visibility"
                    Value="Visible" />
                    <Setter TargetName="Icon"
                    Property="Visibility"
                    Value="Collapsed" />
                -->
                <Setter TargetName="border" Property="Background" Value="{StaticResource MenuItemCheckedBackgroundBrush}" />
                <Setter Property="Foreground" Value="{StaticResource MenuItemCheckedForegroundBrush}" />
            </Trigger>

        </ControlTemplate.Triggers>

    </ControlTemplate>

    <ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">

        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                CornerRadius="{StaticResource Control.Border.CornerRadius}"
                Padding="{TemplateBinding Padding}">
            <StackPanel>
                <ContentPresenter x:Name="icon"
                                  Width="{StaticResource Menu.Icon.Size.Value}"
                                  Height="{StaticResource Menu.Icon.Size.Value}"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  ContentSource="Icon" />
                <StackPanel HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Orientation="Horizontal">
                    <Path x:Name="checkamrk"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="{StaticResource Checkmark}"
                          Fill="{TemplateBinding Foreground}"
                          Visibility="Collapsed" />
                    <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      ContentSource="Header"
                                      RecognizesAccessKey="True" />
                </StackPanel>

                <Popup x:Name="PART_Popup"
                       AllowsTransparency="True"
                       Focusable="false"
                       HorizontalOffset="-9"
                       IsOpen="{TemplateBinding IsSubmenuOpen}"
                       Placement="Bottom"
                       PopupAnimation="None"
                       VerticalOffset="4">
                    <Border Background="{StaticResource MenuItemBackgroundBrush}"
                            BorderBrush="{StaticResource Menu.BorderBrush.Normal}"
                            BorderThickness="1">
                        <ScrollViewer x:Name="SubMenuScrollViewer"
                                      CanContentScroll="true"
                                      Style="{StaticResource {ComponentResourceKey ResourceId=MenuScrollViewer,
                                                                                   TypeInTargetAssembly={x:Type FrameworkElement}}}">
                            <ItemsPresenter Grid.IsSharedSizeScope="true"
                                            KeyboardNavigation.DirectionalNavigation="Cycle"
                                            KeyboardNavigation.TabNavigation="Cycle" />
                        </ScrollViewer>
                    </Border>
                </Popup>

            </StackPanel>
        </Border>

        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="icon" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter TargetName="checkamrk" Property="Visibility" Value="Visible" />
                <Setter TargetName="icon" Property="Visibility" Value="Collapsed" />
            </Trigger>
        </ControlTemplate.Triggers>

    </ControlTemplate>

    <ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">

        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                CornerRadius="0"
                Padding="{TemplateBinding Padding}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="icon" />
                    <ColumnDefinition />
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="igt" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="arrow" />
                </Grid.ColumnDefinitions>

                <ContentPresenter x:Name="icon"
                                  Width="{StaticResource MenuItem.IconSize}"
                                  Height="{StaticResource MenuItem.IconSize}"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  ContentSource="Icon">
                    <ContentPresenter.Margin>
                        <Thickness Right="{StaticResource View.MarginSize.Value}" />
                    </ContentPresenter.Margin>
                </ContentPresenter>

                <Path x:Name="checkmark"
                      Width="{StaticResource MenuItem.IconSize}"
                      Height="{StaticResource MenuItem.IconSize}"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Data="{StaticResource Checkmark}"
                      Fill="{TemplateBinding Foreground}"
                      Visibility="Hidden">
                    <Path.Margin>
                        <Thickness Right="{StaticResource View.MarginSize.Value}" />
                    </Path.Margin>
                </Path>

                <ContentPresenter Grid.Column="1"
                                  HorizontalAlignment="Left"
                                  VerticalAlignment="Center"
                                  ContentSource="Header"
                                  RecognizesAccessKey="True" />
                <TextBlock Grid.Column="3"
                           HorizontalAlignment="Right"
                           VerticalAlignment="Center"
                           Text="{TemplateBinding InputGestureText}" />
            </Grid>
        </Border>

        <ControlTemplate.Triggers>

            <Trigger Property="Command" Value="{x:Null}">
                <Setter Property="IsEnabled" Value="False" />
            </Trigger>

            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="icon" Property="Visibility" Value="Hidden" />
            </Trigger>

            <Trigger Property="IsChecked" Value="true">
                <Setter TargetName="checkmark" Property="Visibility" Value="Visible" />
                <Setter TargetName="icon" Property="Visibility" Value="Hidden" />
            </Trigger>

        </ControlTemplate.Triggers>

    </ControlTemplate>

    <ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                CornerRadius="0">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="icon" />
                    <ColumnDefinition />
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="igt" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="arrow" />
                </Grid.ColumnDefinitions>
                <ContentPresenter x:Name="icon"
                                  Width="{StaticResource MenuItem.IconSize}"
                                  Height="{StaticResource MenuItem.IconSize}"
                                  Margin="2"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  ContentSource="Icon" />
                <Path x:Name="checkmark"
                      Width="{StaticResource MenuItem.IconSize}"
                      Height="{StaticResource MenuItem.IconSize}"
                      Margin="2"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Data="{StaticResource Checkmark}"
                      Fill="{TemplateBinding Foreground}"
                      Visibility="Hidden" />
                <Popup x:Name="PART_Popup"
                       AllowsTransparency="True"
                       Focusable="false"
                       IsOpen="{TemplateBinding IsSubmenuOpen}"
                       Placement="Right"
                       PopupAnimation="None"
                       VerticalOffset="-1">
                    <Border Background="{StaticResource MenuItemBackgroundBrush}"
                            BorderBrush="{StaticResource Menu.BorderBrush.Normal}"
                            BorderThickness="1">
                        <ScrollViewer Name="SubMenuScrollViewer"
                                      CanContentScroll="true"
                                      Style="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement},
                                                                                   ResourceId=MenuScrollViewer}}">
                            <ItemsPresenter Grid.IsSharedSizeScope="true"
                                            KeyboardNavigation.DirectionalNavigation="Cycle"
                                            KeyboardNavigation.TabNavigation="Cycle" />
                        </ScrollViewer>
                    </Border>
                </Popup>

                <ContentPresenter Grid.Column="1"
                                  HorizontalAlignment="Left"
                                  VerticalAlignment="Center"
                                  ContentSource="Header"
                                  RecognizesAccessKey="True" />
                <TextBlock Grid.Column="3"
                           Margin="0 0 4 0"
                           HorizontalAlignment="Right"
                           VerticalAlignment="Center"
                           Text="{TemplateBinding InputGestureText}" />

                <Path Grid.Column="4"
                      Margin="0 0 4 0"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Data="{StaticResource Arrow.Right.Metro}"
                      Fill="{TemplateBinding Foreground}" />

            </Grid>
        </Border>

        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="icon" Property="Visibility" Value="Hidden" />
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter TargetName="checkmark" Property="Visibility" Value="Visible" />
                <Setter TargetName="icon" Property="Visibility" Value="Hidden" />
            </Trigger>
        </ControlTemplate.Triggers>

    </ControlTemplate>

    <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">

        <Setter Property="Background" Value="{StaticResource MenuItemBackgroundBrush}" />
        <Setter Property="BorderBrush" Value="{StaticResource MenuItemBorderBrush}" />
        <Setter Property="Foreground" Value="{StaticResource MenuItemForegroundBrush}" />


        <Setter Property="Padding" Value="{Binding Source={StaticResource View.Region.Spacing.Normal}, Path=Value}" />
        <!--
            <Setter Property="HorizontalContentAlignment"
            Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="VerticalContentAlignment"
            Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        -->
        <Setter Property="ScrollViewer.PanningMode" Value="Both" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />

        <Setter Property="Template" Value="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuItemTemplateKey}}" />

        <Style.Triggers>

            <Trigger Property="Role" Value="TopLevelHeader">
                <Setter Property="Template" Value="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelHeaderTemplateKey}}" />
                <Setter Property="Background" Value="Transparent" />
            </Trigger>

            <Trigger Property="Role" Value="TopLevelItem">
                <Setter Property="Template" Value="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelItemTemplateKey}}" />
                <Setter Property="Background" Value="Transparent" />
            </Trigger>

            <Trigger Property="Role" Value="SubmenuHeader">
                <Setter Property="DockPanel.Dock" Value="Top" />
                <Setter Property="Template" Value="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuHeaderTemplateKey}}" />
            </Trigger>

            <Trigger Property="Role" Value="SubmenuItem">
                <Setter Property="DockPanel.Dock" Value="Top" />
                <Setter Property="Margin">
                    <Setter.Value>
                        <Thickness Bottom="{StaticResource View.Region.Spacing.SmallValue}" Top="{StaticResource View.Region.Spacing.SmallValue}" />
                    </Setter.Value>
                </Setter>
            </Trigger>

            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Background" Value="{StaticResource MenuItemPressedBackgroundBrush}" />
                <Setter Property="BorderBrush" Value="{StaticResource MenuItemPressedBorderBrush}" />
                <Setter Property="Foreground" Value="{StaticResource MenuItemPressedForegroundBrush}" />
            </Trigger>

            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" Value="{StaticResource MenuItemHoverBackgroundBrush}" />
                <Setter Property="BorderBrush" Value="{StaticResource MenuItemHoverBorderBrush}" />
                <Setter Property="Foreground" Value="{StaticResource MenuItemHoverForegroundBrush}" />
            </Trigger>

            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Background" Value="{StaticResource MenuItemDisabledBackgroundBrush}" />
                <Setter Property="BorderBrush" Value="{StaticResource MenuItemDisabledBorderBrush}" />
                <Setter Property="Foreground" Value="{StaticResource MenuItemDisabledForegroundBrush}" />
            </Trigger>

        </Style.Triggers>

    </Style>

</ResourceDictionary>